﻿using SD.Infrastructure.EntityBase;
using System;

namespace SlamDunk.MES.Domain.Entities.ResourceContext
{
    /// <summary>
    /// 物料包装
    /// </summary>
    public class MaterialPack : PlainEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected MaterialPack() { }
        #endregion

        #region 01.创建物料包装构造器
        /// <summary>
        /// 创建物料包装构造器
        /// </summary>
        /// <param name="maxPack">外包装</param>
        /// <param name="maxPackCoefficient">外包装系数</param>
        /// <param name="minPack">内包装</param>
        /// <param name="minPackCoefficient">内包装系数</param>
        public MaterialPack(string maxPack, int? maxPackCoefficient, string minPack, decimal minPackCoefficient)
            : this()
        {
            this.Init(maxPack, maxPackCoefficient, minPack, minPackCoefficient);
        }
        #endregion

        #endregion

        #region # 属性

        #region 外包装 —— string MaxPack
        /// <summary>
        /// 外包装（箱）
        /// </summary>
        public string MaxPack { get; private set; }
        #endregion

        #region 外包装系数 —— int? MaxPackCoefficient
        /// <summary>
        /// 外包装系数
        /// </summary>
        /// <remarks>1箱 = 24袋，24即为MaxPackCoefficient</remarks>
        public int? MaxPackCoefficient { get; private set; }
        #endregion

        #region 内包装 —— string MinPack
        /// <summary>
        /// 内包装（袋）
        /// </summary>
        public string MinPack { get; private set; }
        #endregion

        #region 内包装系数 —— decimal MinPackCoefficient
        /// <summary>
        /// 内包装系数
        /// </summary>
        /// <remarks>1袋 = 2kg，2即为MinPackCoefficient</remarks>
        public decimal MinPackCoefficient { get; private set; }
        #endregion

        #region 外键属性 - 物料定义Id —— Guid MaterialId
        /// <summary>
        /// 外键属性 - 物料定义Id
        /// </summary>
        public Guid MaterialId { get; private set; }
        #endregion

        #region 导航属性 - 物料定义 —— Material Material
        /// <summary>
        /// 导航属性 - 物料定义
        /// </summary>
        public virtual Material Material { get; internal set; }
        #endregion

        #endregion

        #region # 方法

        #region 初始化 —— void Init(string maxPack, int? maxPackCoefficient...
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="maxPack">外包装</param>
        /// <param name="maxPackCoefficient">外包装系数</param>
        /// <param name="minPack">内包装</param>
        /// <param name="minPackCoefficient">内包装系数</param>
        public void Init(string maxPack, int? maxPackCoefficient, string minPack, decimal minPackCoefficient)
        {
            #region # 验证

            if (string.IsNullOrWhiteSpace(minPack))
            {
                throw new ArgumentNullException(nameof(minPack), "内包装不可为空！");
            }
            if (minPackCoefficient <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minPackCoefficient), "内包装系数须大于0！");
            }
            if (!string.IsNullOrWhiteSpace(maxPack) && maxPackCoefficient < 1)
            {
                throw new InvalidOperationException("有外包装时，外包装系数必须大于或等于1！");
            }

            #endregion

            this.MaxPack = maxPack;
            this.MaxPackCoefficient = maxPackCoefficient;
            this.MinPack = minPack;
            this.MinPackCoefficient = minPackCoefficient;
        }
        #endregion

        #endregion
    }
}
